class Api::V1::BrandsController < Api::V1::BaseController
  skip_before_action :authenticate_request!
  before_action :authenticate_request

  def index
    if params[:shop_category_id].presence
      if params[:level].presence && params[:level].to_i == 3
        shop_category = ShopCategory.find params[:shop_category_id]
        @brands = shop_category.parent.parent.brands
      else
        @brands = Brand.where(published: true, shop_category_id: params[:shop_category_id])
      end
    else
      @brands = Brand.all
    end
    @brands = @brands.order("sort ASC").page(params[:page]).per(params[:per])
  end

  def show
    @brand = Brand.find params[:id]
  end

  def spus
    @spus = Spu.where(brand_id: params[:id], state: :onsale, published: true)
    if params[:promotion].presence && params[:promotion]
      @spus = @spus.where(promotion: true)
    end
    if params[:hot].presence && params[:hot]
      @spus = @spus.order("sales_volume DESC")
    end
    if params[:newest].presence && params[:newest]
      @spus = @spus.order("created_at DESC")
    else
      @spus = @spus.order("created_at DESC")
    end
    @spus = @spus.order("created_at DESC").page(params[:page]).per(params[:per])
  end
  
end
